package com.yueqianshop.member.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.yueqianshop.member.exception.PhoneExistException;
import com.yueqianshop.member.exception.UsernameExistException;
import com.yueqianshop.member.feign.CouponFeignService;
import com.yueqianshop.member.vo.MemberLoginVo;
import com.yueqianshop.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yueqianshop.member.entity.MemberEntity;
import com.yueqianshop.member.service.MemberService;
import com.yueqianshop.common.utils.PageUtils;
import com.yueqianshop.common.utils.R;



/**
 * 会员
 *
 * @author cc
 * @email sunlightcs@gmail.com
 * @date 2021-06-05 22:02:38
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    CouponFeignService couponFeignService;


    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){
        MemberEntity entity =  memberService.login(vo);
        System.out.println(entity);
        if(entity!=null){
            return R.ok().setData(entity);
        }else {
            return R.error(15003,"账号或密码错误");
        }


    }



    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo){

        try{
            memberService.regist(vo);
        }catch (PhoneExistException e){
            //(15001,用户存在异常)
            return R.error(15001,"用户存在异常");
        }catch (UsernameExistException e){
            //(15002,手机号存在异常)
            return R.error(15002,"手机号存在异常");
        }

        return R.ok();
    }


    @RequestMapping("/coupons")
    public R testR(){

        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R membercoupons = couponFeignService.membercoupons();
        return R.ok().put("member", memberEntity).put("coupons",  membercoupons.get("coupons"));//张三调用优惠券，张三是member服务，优惠券是远程服务来的
    }





    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
